<?php
namespace app\admin\controller;

use app\common\controller\Admin;

class Apply extends Admin
{
    // 在线升级
    public function upgrade()
    {
        $apply = new \daicuo\Apply();
        if( !$apply->upgradeOnline($this->query['module']) ){
            $error = explode('%',$apply->getError());
            $this->error(lang($error[0]).$error[2],$error[1]);
        }
        $this->success(lang('success'));
    }
    
    // 在线安装
	public function install()
    {
        $apply = new \daicuo\Apply();
        if( !$apply->installOnline($this->query) ){
            $error = explode('%',$apply->getError());
            $this->error(lang($error[0]).$error[2],$error[1]);
		}
        $this->success(lang('success'));
	}
    
    // 浏览器打开应用官网
	public function jump()
    {
		$service = new \daicuo\Service();
        $this->redirect($service->apiUrl().'/home/?'.http_build_query($this->query),302);
	}
    
    // 浏览器下载
    public function down()
    {
        $service = new \daicuo\Service();
        $this->redirect($service->applyDownUrl($this->query),302);
    }
    
    // 禁用插件
    public function disable()
    {
        $apply = new \daicuo\Apply();
        if(!$apply->updateStatus(input('get.module/s'),'disable')){
			$this->error( lang($apply->getError()) );
		}
        $this->success(lang('success'),'apply/index');
    }
    
    // 启用插件
    public function enable()
    {
        $apply = new \daicuo\Apply();
        if(!$apply->updateStatus(input('get.module/s'),'enable')){
			$this->error( lang($apply->getError()) );
		}
        $this->success(lang('success'),'apply/index');
    }
    
    // 卸载插件
    public function remove()
    {
        $apply = new \daicuo\Apply();
        if(!$apply->uninstall(input('get.module/s'))){
			$this->error( lang($apply->getError()) );
		}
        $this->success(lang('success'),'apply/index');
    }
    
    // 应用打包
	public function create()
    {
		return $this->fetch();
	}
    
    // 应用打包保存
    public function save()
    {
        $status = \daicuo\Op::write( input('post.') );
        if( !$status ){
            $this->error(lang('fail'));
        }
        $this->success(lang('success'));
    }
    
    // 删除应用
    public function delete()
    {
        $apply = new \daicuo\Apply();
        if( !$apply->uninstall(input('get.module/s'), true) ){
			$this->error( lang($apply->getError()) );
		}
        $this->success(lang('success'),'apply/index');
    }
    
    // 本地手动安装
	public function update()
    {
        $apply = new \daicuo\Apply();
        if( !$apply->install( input('get.module/s') ) ){
            $error = explode('%',$apply->getError());
            $this->error(lang($error[0]).$error[2],$error[1]);
		}
        $this->success(lang('success'));
	}
    
    //本地应用列表
	public function index()
    {
        $apply   = new \daicuo\Apply();
        $file    = new \files\File();
        $service = new \daicuo\Service();
        //获取已安装的插件列表
        $applys  = $apply->appsInfo();
        //验证已安装的插件是否被手动误删
        foreach($applys as $module=>$value){
            //插件已不存在时删除插件安装列表里对应的记录
            if(!$file->d_has(APP_PATH.$module)){
                $apply->delete($module);
            }
        }
		$this->assign('applys',  $applys);
        $this->assign('api_url', $service->apiUrl());
		return $this->fetch();
	}
}